<template>
  <div class="user-list-container">
    <!-- 搜索区域 -->
    <div class="search-section">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="员工姓名">
          <el-input
            v-model="searchForm.empName"
            placeholder="请输入员工姓名"
            clearable
          />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input
            v-model="searchForm.phoneNumber"
            placeholder="请输入手机号"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">
            搜索
          </el-button>
          <el-button icon="el-icon-refresh" @click="resetSearch"
            >重置</el-button
          >
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd">
            新增用户
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格区域 -->
    <el-table
      v-loading="loading"
      :data="userList"
      border
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column prop="loginName" label="登录名" min-width="120" />
      <el-table-column prop="empName" label="员工姓名" min-width="120" />
      <el-table-column prop="phoneNumber" label="手机号" min-width="120" />
      <el-table-column prop="email" label="邮箱" min-width="180" />
      <el-table-column prop="position" label="职位" min-width="120" />
      <el-table-column prop="salary" label="薪资" min-width="120" />

      <el-table-column label="操作" width="200" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <div class="pagination-section">
      <el-pagination
        :current-page.sync="pageNum"
        :page-size="pageSize"
        :total="total"
        :page-sizes="[5, 10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getEmployeeList, deleteEmployee } from "@/api/employee";

export default {
  name: "UserList",
  data() {
    return {
      // 搜索表单
      searchForm: {
        empName: "",
        phoneNumber: "",
      },
      // 表格数据
      userList: [],
      // 分页参数
      pageNum: 1,
      pageSize: 5,
      total: 0,
      // 加载状态
      loading: false,
      // 选中的用户
      selectedUsers: [],
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    // 获取用户列表
    async getUserList() {
      this.loading = true;
      try {
        const params = {
          pageNum: this.pageNum,
          empName: this.searchForm.empName || undefined,
          phoneNumber: this.searchForm.phoneNumber || undefined,
        };

        console.log("请求参数:", params);
        const res = await getEmployeeList(params);
        console.log("完整响应:", res);

        // 直接使用返回的数据
        this.userList = res.data.list;
        this.total = res.data.total;
        this.pageSize = res.data.pageSize;
      } catch (error) {
        console.error("获取用户列表错误:", error);
        this.$message.error("获取用户列表失败");
      } finally {
        this.loading = false;
      }
    },

    // 搜索
    handleSearch() {
      this.pageNum = 1;
      this.getUserList();
    },

    // 重置搜索
    resetSearch() {
      this.searchForm = {
        empName: "",
        phoneNumber: "",
      };
      this.handleSearch();
    },

    // 删除用户
    async handleDelete(row) {
      try {
        await this.$confirm("确认删除该用户吗？", "提示", {
          type: "warning",
        });

        const res = await deleteEmployee(row.employeeId);
        if (res.code === 200) {
          this.$message.success("删除成功");
          this.getUserList();
        } else {
          this.$message.error(res.message || "删除失败");
        }
      } catch (error) {
        if (error !== "cancel") {
          console.error("删除用户错误:", error);
          this.$message.error("删除失败");
        }
      }
    },

    // 编辑用户
    handleEdit(row) {
      // 跳转到编辑页面
      this.$router.push(`/user/update/${row.employeeId}`);
    },

    // 选择改变
    handleSelectionChange(selection) {
      this.selectedUsers = selection;
    },

    // 每页条数改变
    handleSizeChange(val) {
      this.pageSize = val;
      this.getUserList();
    },

    // 当前页改变
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getUserList();
    },

    // 添加新增方法
    handleAdd() {
      this.$router.push('/user/add');
    },
  },
};
</script>

<style lang="less" scoped>
.user-list-container {
  padding: 20px;

  .search-section {
    background: #fff;
    padding: 18px 20px;
    border-radius: 4px;
    margin-bottom: 16px;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  }

  .el-table {
    margin-bottom: 16px;
  }

  .pagination-section {
    display: flex;
    justify-content: flex-end;
    margin-top: 16px;
    padding: 10px 20px;
    background: #fff;
    border-radius: 4px;
  }
}
</style>